<?php

namespace addons\helper\controller\wxapp\user;

use addons\helper\controller\wxapp\Base;

class User extends Base
{

    protected $relationSearch = true;
    protected $searchFields = 'id,username';
    protected $config = [
        'model'             => '会员',
        'limit'             => 10,
        'defaultSort'       => 'id',
        'defaultOrder'      => 'desc',
        'tabField'          => '',
        'searchPlaceholder' => '输入ID或会员名搜索',
        'multi'             => true,
        'sort'              => true,
        'filter'            => true,
        'search'            => true,
        'field' => [
            'group_id'       => ['所属组别', 'select', true,  false, '', true, true, true],
            'username'       => ['用户名', 'text', true,  true, 'LIKE', true, true, true],
            'nickname'       => ['昵称', 'text', true,  true, 'LIKE', true, true, true],
            'password'       => ['密码', 'password', false,  false, '', false, true, true],
            'email'          => ['电子邮箱', 'text', true,  true, 'LIKE', true, true, true],
            'mobile'         => ['手机号码', 'number', true,  true, 'LIKE', true, true, true],
            'avatar'         => ['头像', 'image', false,  false, '', true, true, true],
            'level'          => ['等级', 'number', true,  true, 'BETWEEN', true, true, true],
            'gender'         => ['性别', 'number', false,  false, '', true, true, true],
            'birthday'       => ['生日', 'date', false,  true, 'RANGE', true, true, true],
            'bio'            => ['格言', 'text', false,  false, '', true, true, true],
            'money'          => ['余额', 'digit', false,  false, '', true, true, true],
            'score'          => ['积分', 'number', false,  false, '', true, true, true],
            'successions'    => ['连续登录天数', 'number', true,  true, 'BETWEEN', true, true, true],
            'maxsuccessions' => ['最大连续登录天数', 'number', true,  true, 'BETWEEN', true, true, true],
            'prevtime'       => ['上次登录时间', 'datetime', false,  false, '', true, true, true],
            'logintime'      => ['登录时间', 'datetime', false,  false, '', true, true, true],
            'loginip'        => ['登录IP', 'text', false,  false, '', true, true, true],
            'loginfailure'   => ['失败次数', 'number', false,  false, '', true, true, true],
            'joinip'         => ['加入IP', 'text', false,  false, '', true, true, true],
            'jointime'       => ['加入时间', 'datetime', true,  true, 'RANGE', true, true, true],
            'createtime'     => ['创建时间', 'datetime', false, false, '', true, false, false],
            'updatetime'     => ['更新时间', 'datetime', false, false, 'RANGE', true, false, false],
            'status'         => ['状态', 'select', false, false, '', true, true, true],
        ],
        'fieldContent' => [
            'group_id' => [],
            'status' => [
                'normal' => '正常',
                'hidden' => '隐藏'
            ]
        ],
        // [可选]前端表单验证规则
        'rules' => [
            'group_id' => [
                'required'    => true
            ],
            'username' => [
                'required'    => true,
                'username'    => true
            ],
            'nickname' => [
                'required'    => true
            ],
            'password' => [
                'required'    => 'add',
                'password'    => true
            ],
            'money' => [
                'required'    => true,
                'money'       => true
            ],
            'score'          => [
                'required'    => true
            ],
            'successions'    => [
                'required'    => true
            ],
            'maxsuccessions' => [
                'required'    => true
            ],
            'prevtime'       => [
                'required'    => true
            ],
            'logintime'      => [
                'required'    => true
            ],
            'loginip'        => [
                'required'    => true
            ],
            'loginfailure'   => [
                'required'    => true
            ],
            'joinip'         => [
                'required'    => true
            ],
            'jointime'       => [
                'required'    => true
            ],
            'status'         => [
                'required'    => true
            ],
        ],
        'auth' => [
            'edit'  => [
                'name' => '编辑',
                'url'  => 'user.user/edit',
            ],
            'multi' => [
                'del'  => [
                    'name'        => '删除',
                    'color'       => '#e74c3c',
                    'icon'        => 'fa-trash',
                    'url'         => 'user.user/del'
                ],
                'hidden'  => [
                    'name'        => '设为隐藏',
                    'color'       => '#f39c12',
                    'icon'        => 'fa-eye-slash',
                    'url'         => 'user.user/multi',
                    'params'      => 'status=hidden'
                ],
                'normal'  => [
                    'name'        => '设为正常',
                    'color'       => '#18bc9c',
                    'icon'        => 'fa-eye',
                    'url'         => 'user.user/multi',
                    'params'      => 'status=normal'
                ],
            ],
            'more' => [
                'del'  => [
                    'name'  => '删除',
                    'color' => '#e74c3c',
                    'url'   => 'user.user/del'
                ],
                'hidden'  => [
                    'name'        => '设为隐藏',
                    'color'       => '#f39c12',
                    'url'         => 'user.user/multi',
                    'params'      => 'status=hidden'
                ],
                'normal'  => [
                    'name'        => '设为正常',
                    'color'       => '#18bc9c',
                    'url'         => 'user.user/multi',
                    'params'      => 'status=normal'
                ],
            ],
        ],
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\User;
        $this->config['field'] = $this->formatField();
        $this->config['pk'] = $this->model->getPk() ? $this->model->getPk() : 'id';

        $this->config['fieldContent']['group_id'] = \app\admin\model\UserGroup::column('id,name');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v) {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);

            $data = [
                'tabList'  => null,
                'dataList' => $result,
                'config'   => $this->config
            ];
            $this->success('ok', $data);
        }
    }

}
